﻿#include "pipelineinfowid.h"
#include <QTimer>
#include "TaskProcess/TaskProcess.h"

#include "ui_pipelineinfowid.h"

PipelineInfoWid::PipelineInfoWid(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::PipelineInfoWid())
{
	ui->setupUi(this);
	this->setWindowFlag(Qt::WindowStaysOnTopHint);
	ui->label_15->setWordWrap(true);
	setFixedWidth(400);
	connect(ui->pushButton, &QPushButton::clicked, this, &PipelineInfoWid::on_pushButton_clicked);
}

PipelineInfoWid::~PipelineInfoWid()
{
	delete ui;
}

void PipelineInfoWid::ShowTaskInfo()
{
	SetPipelineState();
	auto task = m_pipe->GetPipelineTaskInfo();
	if (task.taskId.isEmpty()) {
		ui->groupBox->hide();
		ui->groupBox_3->hide();
		return;
	} else {
		ui->groupBox->show();
		ui->groupBox_3->show();
		ui->label_5->setText(task.taskId);
		ui->spinBox_3->setValue(task.taskNum);
		ui->spinBox_4->setValue(task.finishNum);
		ui->spinBox_2->setValue(task.skuSize.length);
		ui->spinBox_5->setValue(task.skuSize.width);
		ui->spinBox_6->setValue(task.skuSize.height);
		ui->lineEdit_3->setText(task.skuNo);
	}
	this->show();
}

void PipelineInfoWid::UpdateTaskInfo()
{
	auto task = m_pipe->GetPipelineTaskInfo();
	if (task.taskId.isEmpty()) {
		hide();
	} else {
		ui->groupBox->show();
		ui->label_5->setText(task.taskId);
		ui->spinBox_3->setValue(task.taskNum);
		ui->spinBox_4->setValue(task.finishNum);
	}
	SetPipelineState();
}

void PipelineInfoWid::SetPipelineState()
{
	bool updateFlag = true;
	PipelineTaskState state = m_pipe->GetPipelineTaskState();
	QString text;
	ui->groupBox_2->hide();
	switch (state) {
	case PipelineTaskState::pipeline_error:
		text = "业务线执行异常:";
		updateFlag = false;
		ui->groupBox_2->show();
		ui->label_9->setText(m_pipe->GetErrorMsg());
		break;
	case PipelineTaskState::pipeline_task_idle:
		text = "业务线空闲";
		break;
	case PipelineTaskState::pipeline_task_receive:
		text = "业务线收到任务";
		break;
	case PipelineTaskState::pipeline_task_inited:
		text = "业务线任务初始化完成";
		break;
	case PipelineTaskState::pipeline_task_start:
		text = "业务线任务开始";
		break;
	case PipelineTaskState::pipeline_task_stepPrepare:
		text = "业务线节拍准备中";
		break;
	case PipelineTaskState::pipeline_task_stepStart:
		text = "业务线节拍准备中";
		break;
	case PipelineTaskState::pipeline_task_wait_finish:
		text = "业务线等待节拍执行结束";
		break;
	case PipelineTaskState::pipeline_task_finish:
		text = "业务线任务完成";
		break;
	default:
		text = "业务线状态异常";
		break;
	}
	ui->label_15->setText(text);
	if (updateFlag) {
		QTimer::singleShot(1000, [&]() {
			this->UpdateTaskInfo();
			});
	}
}


void PipelineInfoWid::Init(TaskProcess* pipeM, Pipeline* pipe)
{
	m_pipe = pipe;
	m_pipeM = pipeM;
	auto info = pipe->GetPipelinenfo();
	ui->label_2->setText(info.id);
	ui->lineEdit->setText(info.grab);
	ui->lineEdit_2->setText(info.putdown);
	ShowTaskInfo();
	m_detail = new PipelineDetailWid(m_pipe, this);
}

void PipelineInfoWid::on_pushButton_clicked()
{
	m_pipeM->PipelineErrorClear(m_pipe);
	UpdateTaskInfo();
}
